package com.sk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sk.entity.Equipment;
import com.sk.entity.Inventory;
import com.sk.mapper.EquipmentMapper;
import com.sk.mapper.InventoryMapper;
import com.sk.service.IEquipmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sk.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author smith
 * @since 2025-05-27
 */
@Service
public class EquipmentServiceImpl  implements IEquipmentService {

    @Autowired
    private EquipmentMapper equipmentMapper;


    @Override
    public Result getById(Integer equipmentId) {
        Equipment equipment =equipmentMapper.selectById(equipmentId);
        if(equipment !=null)
            return Result.getSuccess(equipment);
        return Result.getFail(null);
    }

    @Override
    public Result getAll() {
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);  // 只查询status=1的装备
        List<Equipment> equipmentList = equipmentMapper.selectList(queryWrapper);
        if (equipmentList != null && !equipmentList.isEmpty()) {
            return Result.getSuccess(equipmentList);
        } else {
            return Result.getFail("暂无装备数据");
        }
    }




    @Override
    public Result getByTypeId(Integer typeId) {
        QueryWrapper<Equipment> wrapper =new QueryWrapper<>();
        wrapper.eq("type_id",typeId).eq("status", 1);
        List<Equipment> equipmentList =equipmentMapper.selectList(wrapper);
        if(equipmentList !=null &&equipmentList.size()>0)
            return Result.getSuccess(equipmentList);
        else
            return Result.getFail("暂无该类别的装备");
    }


    //根据名称模糊查询装备
    public Result getByName(String equipmentName) {
        QueryWrapper<Equipment> wrapper = new QueryWrapper<>();
        wrapper.like("equipment_Name",equipmentName).eq("status", 1);
        List<Equipment> equipmentList = equipmentMapper.selectList(wrapper);
        if(equipmentList !=null && equipmentList.size() >0)
            return Result.getSuccess(equipmentList);
        else
            return Result.getFail("null");
    }


    //根据用户Id查询饰品信息
    @Override
    public Result getByuserId(Integer userId) {
        QueryWrapper<Equipment> wrapper =new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<Equipment> equipmentList =equipmentMapper.selectList(wrapper);
        if(equipmentList !=null &&equipmentList.size()>0)
            return Result.getSuccess(equipmentList);
        else
            return Result.getFail("暂无该类别的装备");
    }

    @Override
    public Result update(Equipment equipment) {
        equipment.setCreateTime(LocalDateTime.now());
        int rows = equipmentMapper.updateById(equipment);
        if (rows > 0) {
            return Result.updateSuccess(equipment);
        }
        return Result.updateFail(equipment);
    }

}
